// 主页鉴权功能
import router from "./router";
import nprogress from "nprogress";
import "nprogress/nprogress.css";
import store from "./store";
// 路由前置守卫
//白名单 不需要token就可以登录的  login/404
const whileList = ["/login", "/404"];
router.beforeEach(async (to, from, next) => {
  nprogress.start();
  if (store.getters.token) {
    // 存在token
    if (to.path === "/login") {
      next("/"); //跳转到首页
      nprogress.done();
    } else {
      // 判断是否获取或资料
      if (!store.getters.userId) {
        await store.dispatch("user/getUserInfo");
      }
      next();
    }
  } else {
    // 不存在token
    if (to.path === "/login" && "/404") {
      next();
    } else {
      next("/login");
      nprogress.done();
    }
  }
  //  if (whileList.includes(to.path)) {
  //     next();
  //   } else {
  //     next("/login");
  //     nprogress.done();
  //   }
});

// 路由后置守卫
router.afterEach(() => {
  nprogress.done();
});
